# coding=utf-8
# 邮件发送组件
import smtplib
from email.mime.text import MIMEText
from ..logger.familyLogger import FamilyLogger


class QQMail(object):
    """QQ邮箱服务"""
    logger = FamilyLogger().getLogger("QQMail")
    msg_from = ''
    pass_key = ''
    msg_to = ''

    def __init__(self,msg_from,pass_key,msg_to):
        """ 邮箱服务初始化"""
        self.msg_from = msg_from
        self.pass_key = pass_key
        self.msg_to = msg_to

    def send(self,subject,content):
        """发送邮件"""
        msg = MIMEText(content)
        msg['Subject'] = subject
        msg['From'] = self.msg_from
        msg['To'] = self.msg_to
        try:
            s = smtplib.SMTP_SSL("smtp.qq.com", 465)
            s.login(self.msg_from, self.pass_key)
            s.sendmail(self.msg_from, self.msg_to, msg.as_string())
            self.logger.info("邮件发送成功[to:"+self.msg_to+"]")
        except s.SMTPException, e:
            self.logger.error("邮件发送失败[to:" + self.msg_to + "]")
        finally:
            s.quit()


